package com.entities.projecttask.servlets;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.core.hibernate.exceptionevent.ExceptionEvent;
import com.core.protocols.IKVObject;
import com.core.protocols.IServiceLayerDelegate;
import com.core.servlets.TaskTrackerServlet;
import com.core.utils.GlobalConstants;
import com.core.utils.ListUtils;
import com.core.utils.RequestParams;
import com.entities.projecttask.requestmapparser.ProjectTaskMapParser;
import com.entities.projecttask.servicelayer.ProjectTasksListLayer;
import com.model.project.ProjectTask;

/**
 * Servlet implementation class ProjectAdd
 */

public class ProjectTaskAdd
        extends TaskTrackerServlet
        implements IServiceLayerDelegate {

	private static final long   serialVersionUID = 1L;
	private HttpServletResponse postResponse     = null;
	private Integer             parentProjectId  = null;



	public ProjectTaskAdd() {

	}



	@Override
	protected void setup () {

		super.setup();
		events.add(new ExceptionEvent(ExceptionEvent.TEMPLATEEVENT));

	}



	@Override
	protected void doPost ( HttpServletRequest request,
	                        HttpServletResponse response )
	        throws ServletException, IOException {

		this.postResponse = response;
		ProjectTask template = null;
		RequestParams requestParams = RequestParams.paramsFromRequest(request);
		template = ProjectTaskMapParser.fromMap(requestParams);

		template.setCreateDate(new Date());

		ProjectTasksListLayer projectTasksListLayer = new ProjectTasksListLayer();
		projectTasksListLayer.setLayerDelegate(this);
		projectTasksListLayer.addProjectTask(template, this.parentProjectId);

		template = null;

	}



	@Override
	protected void doGet ( HttpServletRequest request,
	                       HttpServletResponse response )
	        throws ServletException, IOException {

		this.parentProjectId = RequestParams.paramsFromRequest(request).integerAtIndexForKey(0, "id");

		super.doGet(request, response);
	}



	@Override
	public <T> void responseResponseItemsList ( final List <T> items,
	                                            final Object request,
	                                            final Exception exception ) {

		T item = ListUtils.listItemAtIndex(items, 0);

		this.redirectItemIntoOpenServlet(item);

	}



	private <T> void redirectItemIntoOpenServlet ( T item ) {

		if ( this.postResponse == null ) {
			return;
		}

		String redirection = String.format("%s", GlobalConstants.SERVLET_PROJECTLIST);

		if ( item != null ) {
			if ( item instanceof IKVObject ) {
				IKVObject kvObject = (IKVObject) item;
				String idValue = kvObject.idValue().toString();
				redirection = String.format("%s?%s", GlobalConstants.SERVLET_TASKINFO, idValue);
			}
		}

		try {
			this.postResponse.sendRedirect(redirection);
		} catch ( IOException e ) {
			e.printStackTrace();
		}

	}

}
